import {store} from "./../redux/index"
import {Navigate, useLocation} from 'react-router-dom'
import {AxiosCanceler} from "../api/helper/axiosCancel";
import {searchRoute} from "./utils";
import {rootRouter} from "../router";

const axiosCanceler = new AxiosCanceler();


const AuthRouter = (props ) => {
    const {pathname}  = useLocation();
    let route = searchRoute(pathname, rootRouter);
    // * 在跳转路由之前，清除所有的请求
    axiosCanceler.removeAllPending();

    let {global} = store.getState();
    // console.log(route, '提示2---2', route)
    // * 判断当前路由是否需要访问权限(不需要权限直接放行)
    if (!route.meta?.requiresAuth) return props.children;

    if(!global.token) {
        return <Navigate to={'/login'} replace/>;
    }
    return props.children;

}

export default AuthRouter